<?php
/**
 ** added by lss@2017/07/15
 ** 获取最新一期开奖数据，比对数据库，生成中/不中记录 -> 单个公式的比对和记录
**/
set_time_limit(0);
error_reporting(E_ALL);
date_default_timezone_set("PRC");
include('./db_config.php');
include('./function.php');
//print_r($argv);
$type = 20; //北京赛车对应数据库中的dat_type
$algo_id = $argv[1];
$lottery_expect = $argv[2];
$lottery_code = $argv[3];

//测试用数据
/*
$lottery_expect = '628708';
$lottery_code = '02,05,04,10,03,07,01,09,06,08';
*/


//1.判断参数合理性
if(!$algo_id || !is_numeric($algo_id))
{
	exit('参数缺失');
}

if(intval($lottery_expect)<=0)
{
	exit('开奖期数参数错误');
}

$lottery_code_array = explode(',',$lottery_code);

if(count($lottery_code_array) != 10)
{
	exit('开奖数据参数错误');
}

//2.开奖期数号码处理->格式化，保证前面有0，不然判断是否在数组中时会出现错误
$lottery_code_array = explode(',',$lottery_code);

/*
foreach($lottery_code_array as &$lottery_code_single)
{
	$lottery_code_single = ltrim($lottery_code_single,'0');
	if(strlen($lottery_code_single) <2)
	{
		$lottery_code_single='0'.$lottery_code_single;
	}
}
*/


$first_code = ltrim($lottery_code_array[0],'0');
$second_code = ltrim($lottery_code_array[1],'0');
$third_code = ltrim($lottery_code_array[2],'0');
$four_code = ltrim($lottery_code_array[3],'0');
$five_code = ltrim($lottery_code_array[4],'0');
$six_code = ltrim($lottery_code_array[5],'0');
$seven_code = ltrim($lottery_code_array[6],'0');
$eight_code = ltrim($lottery_code_array[7],'0');
$nine_code = ltrim($lottery_code_array[8],'0');
$ten_code = ltrim($lottery_code_array[9],'0');

foreach(array('first_code','second_code','third_code','four_code','five_code','six_code','seven_code','eight_code','nine_code','ten_code') as $code_single)
{
	if(intval(${$code_single}) <10)
	{
		${$code_single} = '0'.${$code_single};
	}
}

//开奖期数号码处理->格式化，保证前面有0，不然判断是否在数组中时会出现错误  结束

//1.连接数据库->pdo方式
$db_pdo_link = pdo_write_read_link();

//2.定义计划种类[function.php中有定义]

//3.数据对比  
foreach($guess_type_array as $guess_type_single)
{
	$sql = "select * from pk10_referral_guess_new where guess_type_plan='{$algo_id}' and guess_type='{$guess_type_single}' and exp_first<='{$lottery_expect}' and exp_last>='{$lottery_expect}' limit 1";
	
	//echo $sql;
	//echo '\r';
	
	$data_result = $db_pdo_link->query($sql);
	
	$list_single = $data_result->fetch(PDO::FETCH_ASSOC);
		
	//第一个号到第十个号的处理逻辑一样
	if($guess_type_single == 'gjw')  //第一个号
	{
		$dat_codes_array = explode(',',$list_single['dat_codes']);
		if(in_array($first_code,$dat_codes_array))  //中了
		{
			setPk10NextGuess($db_pdo_link,$algo_id,$guess_type_single,$lottery_expect,$list_single['guess_id'],$plan_result=true,$first_code);
		}else
		{
			//没中 还在期数内 不做任何处理
			if((intval($list_single['exp_last']) - intval($lottery_expect)) >=1)
			{
				continue;
			}else
			{
				//没中 也到三期结束  设置为错 添加一条新数据
				setPk10NextGuess($db_pdo_link,$algo_id,$guess_type_single,$lottery_expect,$list_single['guess_id'],$plan_result=false,$first_code);
			}
		}
	}else if($guess_type_single == 'yjw')  //第二个号  
	{
		$dat_codes_array = explode(',',$list_single['dat_codes']);
		if(in_array($second_code,$dat_codes_array))  //中了
		{
			setPk10NextGuess($db_pdo_link,$algo_id,$guess_type_single,$lottery_expect,$list_single['guess_id'],$plan_result=true,$second_code);
		}else
		{
			//没中 还在期数内 不做任何处理
			if((intval($list_single['exp_last']) - intval($lottery_expect)) >=1)
			{
				continue;
			}else
			{
				//没中 也到三期结束  设置为错 添加一条新数据
				setPk10NextGuess($db_pdo_link,$algo_id,$guess_type_single,$lottery_expect,$list_single['guess_id'],$plan_result=false,$second_code);
			}
		}
		
	}else if($guess_type_single == 'jjw')  //第三个号
	{
		$dat_codes_array = explode(',',$list_single['dat_codes']);
		if(in_array($third_code,$dat_codes_array))  //中了
		{
			setPk10NextGuess($db_pdo_link,$algo_id,$guess_type_single,$lottery_expect,$list_single['guess_id'],$plan_result=true,$third_code);
		}else
		{
			//没中 还在期数内 不做任何处理
			if((intval($list_single['exp_last']) - intval($lottery_expect)) >=1)
			{
				continue;
			}else
			{
				//没中 也到三期结束  设置为错 添加一条新数据
				setPk10NextGuess($db_pdo_link,$algo_id,$guess_type_single,$lottery_expect,$list_single['guess_id'],$plan_result=false,$third_code);
			}
		}
	}else if($guess_type_single == 'sjw')  //第四个号
	{
		$dat_codes_array = explode(',',$list_single['dat_codes']);
		if(in_array($four_code,$dat_codes_array))  //中了
		{
			setPk10NextGuess($db_pdo_link,$algo_id,$guess_type_single,$lottery_expect,$list_single['guess_id'],$plan_result=true,$four_code);
		}else
		{
			//没中 还在期数内 不做任何处理
			if((intval($list_single['exp_last']) - intval($lottery_expect)) >=1)
			{
				continue;
			}else
			{
				//没中 也到三期结束  设置为错 添加一条新数据
				setPk10NextGuess($db_pdo_link,$algo_id,$guess_type_single,$lottery_expect,$list_single['guess_id'],$plan_result=false,$four_code);
			}
		}
		
	}else if($guess_type_single == 'wjw')  //第五个号
	{
		$dat_codes_array = explode(',',$list_single['dat_codes']);
		if(in_array($five_code,$dat_codes_array))  //中了
		{
			setPk10NextGuess($db_pdo_link,$algo_id,$guess_type_single,$lottery_expect,$list_single['guess_id'],$plan_result=true,$five_code);
		}else
		{
			//没中 还在期数内 不做任何处理
			if((intval($list_single['exp_last']) - intval($lottery_expect)) >=1)
			{
				continue;
			}else
			{
				//没中 也到三期结束  设置为错 添加一条新数据
				setPk10NextGuess($db_pdo_link,$algo_id,$guess_type_single,$lottery_expect,$list_single['guess_id'],$plan_result=false,$five_code);
			}
		}
		
	}else if($guess_type_single == 'ljw')  //第六个号
	{
		$dat_codes_array = explode(',',$list_single['dat_codes']);
		if(in_array($six_code,$dat_codes_array))  //中了
		{
			setPk10NextGuess($db_pdo_link,$algo_id,$guess_type_single,$lottery_expect,$list_single['guess_id'],$plan_result=true,$six_code);
		}else
		{
			//没中 还在期数内 不做任何处理
			if((intval($list_single['exp_last']) - intval($lottery_expect)) >=1)
			{
				continue;
			}else
			{
				//没中 也到三期结束  设置为错 添加一条新数据
				setPk10NextGuess($db_pdo_link,$algo_id,$guess_type_single,$lottery_expect,$list_single['guess_id'],$plan_result=false,$six_code);
			}
		}
		
	}else if($guess_type_single == 'qjw')  //第七个号
	{
		$dat_codes_array = explode(',',$list_single['dat_codes']);
		if(in_array($seven_code,$dat_codes_array))  //中了
		{
			setPk10NextGuess($db_pdo_link,$algo_id,$guess_type_single,$lottery_expect,$list_single['guess_id'],$plan_result=true,$seven_code);
		}else
		{
			//没中 还在期数内 不做任何处理
			if((intval($list_single['exp_last']) - intval($lottery_expect)) >=1)
			{
				continue;
			}else
			{
				//没中 也到三期结束  设置为错 添加一条新数据
				setPk10NextGuess($db_pdo_link,$algo_id,$guess_type_single,$lottery_expect,$list_single['guess_id'],$plan_result=false,$seven_code);
			}
		}
	}else if($guess_type_single == 'bjw')  //第八个号
	{
		$dat_codes_array = explode(',',$list_single['dat_codes']);
		if(in_array($eight_code,$dat_codes_array))  //中了
		{
			setPk10NextGuess($db_pdo_link,$algo_id,$guess_type_single,$lottery_expect,$list_single['guess_id'],$plan_result=true,$eight_code);
		}else
		{
			//没中 还在期数内 不做任何处理
			if((intval($list_single['exp_last']) - intval($lottery_expect)) >=1)
			{
				continue;
			}else
			{
				//没中 也到三期结束  设置为错 添加一条新数据
				setPk10NextGuess($db_pdo_link,$algo_id,$guess_type_single,$lottery_expect,$list_single['guess_id'],$plan_result=false,$eight_code);
			}
		}
		
	}else if($guess_type_single == 'njw')  //第九个号
	{
		$dat_codes_array = explode(',',$list_single['dat_codes']);
		if(in_array($nine_code,$dat_codes_array))  //中了
		{
			setPk10NextGuess($db_pdo_link,$algo_id,$guess_type_single,$lottery_expect,$list_single['guess_id'],$plan_result=true,$nine_code);
		}else
		{
			//没中 还在期数内 不做任何处理
			if((intval($list_single['exp_last']) - intval($lottery_expect)) >=1)
			{
				continue;
			}else
			{
				//没中 也到三期结束  设置为错 添加一条新数据
				setPk10NextGuess($db_pdo_link,$algo_id,$guess_type_single,$lottery_expect,$list_single['guess_id'],$plan_result=false,$nine_code);
			}
		}
		
	}else if($guess_type_single == 'tjw')  //第十个号
	{
		$dat_codes_array = explode(',',$list_single['dat_codes']);
		if(in_array($ten_code,$dat_codes_array))  //中了
		{
			setPk10NextGuess($db_pdo_link,$algo_id,$guess_type_single,$lottery_expect,$list_single['guess_id'],$plan_result=true,$ten_code);
		}else
		{
			//没中 还在期数内 不做任何处理
			if((intval($list_single['exp_last']) - intval($lottery_expect)) >=1)
			{
				continue;
			}else
			{
				//没中 也到三期结束  设置为错 添加一条新数据
				setPk10NextGuess($db_pdo_link,$algo_id,$guess_type_single,$lottery_expect,$list_single['guess_id'],$plan_result=false，,$ten_code);
			}
		}
		
	}else if($guess_type_single == 'q2zx')  //前二直选
	{
		$code_datas_array = explode('*',$list_single['dat_codes']);

		if(in_array($first_code,explode(',',$code_datas_array[0])) && in_array($second_code,explode(',',$code_datas_array[1])))
		{
			 setPk10NextGuess($db_pdo_link,$algo_id,$guess_type_single,$lottery_expect,$list_single['guess_id'],$plan_result=true);
		}else
		{
			//没中 还在期数内 不做任何处理
			if((intval($list_single['exp_last']) - intval($lottery_expect)) >=1)
			{
				continue;
			}else
			{
				//没中 也到三期结束  设置为错 添加一条新数据
				setPk10NextGuess($db_pdo_link,$algo_id,$guess_type_single,$lottery_expect,$list_single['guess_id'],$plan_result=false);
			}
		}
	}else if($guess_type_single == 'q3zx')  //前三直选
	{
		$code_datas_array = explode('*',$list_single['dat_codes']);
		if(in_array($first_code,explode(',',$code_datas_array[0])) && in_array($second_code,explode(',',$code_datas_array[1])) && in_array($third_code,explode(',',$code_datas_array[2])))
		{
			 setPk10NextGuess($db_pdo_link,$algo_id,$guess_type_single,$lottery_expect,$list_single['guess_id'],$plan_result=true);
		}else
		{
			//没中 还在期数内 不做任何处理
			if((intval($list_single['exp_last']) - intval($lottery_expect)) >=1)
			{
				continue;
			}else
			{
				//没中 也到三期结束  设置为错 添加一条新数据
				setPk10NextGuess($db_pdo_link,$algo_id,$guess_type_single,$lottery_expect,$list_single['guess_id'],$plan_result=false);
			}
		}
	}else if($guess_type_single == 'gyhz')  //冠亚和值
	{
		$dat_codes_array = explode(',',$list_single['dat_codes']);
		$first_code = ltrim($first_code,'0');
		$second_code = ltrim($second_code,'0');
		$guan_ya_sum = intval($first_code) + intval($second_code);

		if($guan_ya_sum <10)
		{
			$guan_ya_sum = '0'.$guan_ya_sum;
		}

		if(in_array($guan_ya_sum,$dat_codes_array))
		{
			 setPk10NextGuess($db_pdo_link,$algo_id,$guess_type_single,$lottery_expect,$list_single['guess_id'],$plan_result=true);
		}else
		{
			//没中 还在期数内 不做任何处理
			if((intval($list_single['exp_last']) - intval($lottery_expect)) >=1)
			{
				continue;
			}else
			{
				//没中 也到三期结束  设置为错 添加一条新数据
				setPk10NextGuess($db_pdo_link,$algo_id,$guess_type_single,$lottery_expect,$list_single['guess_id'],$plan_result=false);
			}
		}	
	}else if($guess_type_single == 'gyhzdx')  //冠亚和值大小
	{
		$guan_ya_sum = intval(ltrim($first_code,'0')) + intval(ltrim($second_code,'0'));
		if((intval($guan_ya_sum)>=12 && $list_single['dat_codes'] == '1') || (intval($guan_ya_sum)<12 && $list_single['dat_codes'] == '0'))   //12位临界点
		{
			setPk10NextGuess($db_pdo_link,$algo_id,$guess_type_single,$lottery_expect,$list_single['guess_id'],$plan_result=true);
		}else
		{
			//没中 还在期数内 不做任何处理
			if((intval($list_single['exp_last']) - intval($lottery_expect)) >=1)
			{
				continue;
			}else
			{
				//没中 也到三期结束  设置为错 添加一条新数据
				setPk10NextGuess($db_pdo_link,$algo_id,$guess_type_single,$lottery_expect,$list_single['guess_id'],$plan_result=false);
			}
		}
		
	}else if($guess_type_single == 'gyhzds')  //冠亚和值单双
	{
		$guan_ya_sum = intval(ltrim($first_code,'0')) + intval(ltrim($second_code,'0'));
		$dan_shuang_result = $guan_ya_sum%2;
		if(($dan_shuang_result=='1' && $list_single['dat_codes'] == '1') || ($dan_shuang_result=='0' && $list_single['dat_codes'] == '0'))
		{
			setPk10NextGuess($db_pdo_link,$algo_id,$guess_type_single,$lottery_expect,$list_single['guess_id'],$plan_result=true);
		}else
		{
			//没中 还在期数内 不做任何处理
			if((intval($list_single['exp_last']) - intval($lottery_expect)) >=1)
			{
				continue;
			}else
			{
				//没中 也到三期结束  设置为错 添加一条新数据
				setPk10NextGuess($db_pdo_link,$algo_id,$guess_type_single,$lottery_expect,$list_single['guess_id'],$plan_result=false);
			}
		}
	}
}


/**
 ** 设置下一期的中奖计划 [pk10的]
 ** @$db_pdo_link[数据库连接对象]
 ** @$algo_id [哪个公式  目前为  1-1000]
 ** @$type[属于哪个计划的 冠军计划 前二直选 前三直选 冠亚和值... 15个计划]
 ** @$lottery_expect [pk10当前的开奖期数]
 ** @$guess_id [要更新的期数]
 ** @$plan_result [true 中了  false 没中]
 ** @param $dat_code[string 1-10要用到的判断单双/大小的  最新开奖数据的第几球]
**/
function setPk10NextGuess($db_pdo_link,$algo_id,$type,$lottery_expect,$guess_id,$plan_result,$dat_codes='')
{
	//print_r($algo_id.' '.$type.' '.$lottery_expect.' '.$guess_id.' '.$plan_result);
  
	$check_sql = "select guess_id,dx_dat_codes,ds_dat_codes from  pk10_referral_guess_new where guess_id='{$guess_id}' and bingo_expect!='0'";
	$countInfo_result = $db_pdo_link->query($check_sql);
	$countInfo = $countInfo_result->fetch(PDO::FETCH_ASSOC);
	
	if(!empty($countInfo))  //查询到数据的话，说明已经改过了
	{
		return;
	}
	
	//临时加的，后续要优化此段代码
	$check_sql = "select guess_id,dx_dat_codes,ds_dat_codes from  pk10_referral_guess_new where guess_id='{$guess_id}'";
	$countInfo_result = $db_pdo_link->query($check_sql);
	$countInfo = $countInfo_result->fetch(PDO::FETCH_ASSOC);
	
	$dx_guess_result = '0';
	$ds_guess_result = '0';  //1为中,0为不中
	
	if(in_array($type,array('gjw','yjw','jjw','sjw','wjw','ljw','qjw','bjw','njw','tjw')))
	{
		$dat_codes = ltrim($dat_codes,'0');
		//0为小，1为大
		if(($dat_codes >=6  &&  $countInfo['dx_dat_codes'] == '1') || ($dat_codes <6  &&  $countInfo['dx_dat_codes'] == '0'))
		{
			$dx_guess_result = '1';  //1为中
		}
		
		//0为双, 1为单
		if(($dat_codes % 2 == 1 && $countInfo['ds_dat_codes'] == '1') || ($dat_codes % 2 == 0 && $countInfo['ds_dat_codes'] == '0'))
		{
			$ds_guess_result = '1'; //1为中
		}
		
		$codes = getRandomCode(1,10,5);
		$recom_yards  = '5';
	}else if($type == 'q2zx')
	{
		$codes = getRandomCode(1,10,7);
		$codes.='*'.getRandomCode(1,10,7);
		$recom_yards  = '7';
	}else if($type == 'q3zx')
	{
		$codes = getRandomCode(1,10,8);
		$codes.='*'.getRandomCode(1,10,8);
		$codes.='*'.getRandomCode(1,10,8);
		$recom_yards  = '8';
	}else if($type == 'gyhz')
	{
		$codes = getRandomCode(3,19,8);
		$recom_yards  = '8';
	}else if($type == 'gyhzdx')
	{
		$codes = rand('0','1');  //0为小  1为大 
		$recom_yards  = '1';		
	}else if($type == 'gyhzds')
	{
		$codes = rand('0','1');  //0为双  1为单
		$recom_yards  = '1';
	}
	//冠军计划--猜中 【中 新加一条数据  不中 新加一条数据 】
	if($plan_result == true)
	{
		$update_sql = "update pk10_referral_guess_new set guess_result=1,ds_guess_result='{$ds_guess_result}',dx_guess_result='{$dx_guess_result}',bingo_expect='{$lottery_expect}',guess_type_plan='{$algo_id}',bingo_diff={$lottery_expect}-exp_first where guess_id='{$guess_id}'";
	}else
	{
		$update_sql = "update pk10_referral_guess_new set guess_result='0',ds_guess_result='{$ds_guess_result}',dx_guess_result='{$dx_guess_result}',bingo_expect='{$lottery_expect}',guess_type_plan='{$algo_id}',bingo_diff={$lottery_expect}-exp_first where guess_id='{$guess_id}'";
	}
		//echo $update_sql;
		$update_result = $db_pdo_link->exec($update_sql);
		
		//echo $update_sql;
		//echo '<br/>';
	$first =  $lottery_expect+1;
	$last = $lottery_expect+3;
	$time = date('Y-m-d H:i:s');	
	
	$dx_codes = rand('0','1'); 
	$ds_codes = rand('0','1'); 
	
	$insert_sql = "insert into `pk10_referral_guess_new`(exp_first,exp_last,dat_codes,guess_type_plan,guess_type,recom_yards,addtime,dx_dat_codes,ds_dat_codes,dx_guess_result,ds_guess_result) values({$first},{$last},'{$codes}','{$algo_id}','{$type}','{$recom_yards}','{$time}','{$dx_codes}','{$ds_codes}','0','0')";
	
	//echo $insert_sql;
	//echo '<br/>';
	
	$insert_result = $db_pdo_link->exec($insert_sql);
	
	//重新计算近十,二十,三十,五十期的概率
	$check_sql = "select guess_id,guess_result from  pk10_referral_guess_new where guess_type_plan='{$algo_id}' and guess_type='{$type}' order by guess_id desc limit 1,50";
	$check_result = $db_pdo_link->query($check_sql);
	$check_data = $check_result->fetchAll(PDO::FETCH_ASSOC);
	
	$check_data_10 = array_slice($check_data,0,10,true);
	$check_data_20 = array_slice($check_data,0,20,true);
	$check_data_30 = array_slice($check_data,0,30,true);
		
	$check_bingo = '0';
	$check_total = '0';
	
	//50期
	$check_bingo_50  =  $check_bingo_30 = $check_bingo_20 = $check_bingo_10 = '0';
	$check_total_50 = $check_total_30 = $check_total_20 = $check_total_10 = '0';
	foreach($check_data as $check_single)
	{
		$check_total_50 ++;
		if($check_single['guess_result'] == '1')
		{
			$check_bingo_50++;
		}
	}
	//重新计算近50期的概率  结束
	$pro_result_50 = round(($check_bingo_50/$check_total_50)*100 ,2);	
	
	//30期
	foreach($check_data_30 as $check_single_30)
	{
		$check_total_30 ++;
		if($check_single_30['guess_result'] == '1')
		{
			$check_bingo_30++;
		}
	}
	//重新计算近30期的概率  结束
	$pro_result_30 = round(($check_bingo_30/$check_total_30)*100 ,2);

	//20期
	foreach($check_data_20 as $check_single_20)
	{
		$check_total_20 ++;
		if($check_single_20['guess_result'] == '1')
		{
			$check_bingo_20++;
		}
	}
	//重新计算近20期的概率  结束
	$pro_result_20 = round(($check_bingo_20/$check_total_20)*100 ,2);	
	
	//10期
	foreach($check_data_10 as $check_single_10)
	{
		$check_total_10 ++;
		if($check_single_10['guess_result'] == '1')
		{
			$check_bingo_10++;
		}
	}
	//重新计算近10期的概率  结束
	$pro_result_10 = round(($check_bingo_10/$check_total_10)*100 ,2);	
	
	$pro_update_sql = "update `pk10_referral_probability` set pro_result='{$pro_result_50}' where guess_type_plan='{$algo_id}' and guess_type='{$type}' and data_period='5'";
	$proba_update_result = $db_pdo_link->exec($pro_update_sql);
	
	$pro_update_sql_30 = "update `pk10_referral_probability` set pro_result='{$pro_result_30}' where guess_type_plan='{$algo_id}' and guess_type='{$type}' and data_period='3'";
	$proba_update_result = $db_pdo_link->exec($pro_update_sql_30);
	
	$pro_update_sql_20 = "update `pk10_referral_probability` set pro_result='{$pro_result_20}' where guess_type_plan='{$algo_id}' and guess_type='{$type}' and data_period='2'";
	$proba_update_result = $db_pdo_link->exec($pro_update_sql_20);
	
	$pro_update_sql_10 = "update `pk10_referral_probability` set pro_result='{$pro_result_10}' where guess_type_plan='{$algo_id}' and guess_type='{$type}' and data_period='1'";
	$proba_update_result = $db_pdo_link->exec($pro_update_sql_10);
	
	
	//echo $proba_update_result;
}

?>
